﻿#ifndef CHATTEXTVIEWER_H
#define CHATTEXTVIEWER_H

#include <QTextBrowser>

class ChatTextViewer : public QTextBrowser
{
    Q_OBJECT
public:
    explicit ChatTextViewer(QWidget *parent = Q_NULLPTR);
    int showChat(bool fromSelf, const QPixmap &icon, const QString &name, const QString &content, qint64 timestamp = 0);
    int showImage(const QPixmap &icon, const QString &name, const QUrl &resourceName, const QPixmap &pixmap);
    int showImage(bool fromSelf, const QPixmap &icon, const QString &name, const QUrl &url, qint64 timestamp = 0);
    bool initRowState(int row, const QString &text, const QByteArray& mid = QByteArray());
    void initMid(const QByteArray& mid, int row, const QString &text);
    void setMidState(const QByteArray& mid, const QString &text);
public slots:
    void clearAll();
protected:
    void contextMenuEvent(QContextMenuEvent *e);
private slots:
    void downloadRequested(QString url);
    void downloadFinished(QByteArray buffer,QVariant userData);
    void downloadError(QVariant userData);
    void downloadProgress(qint64 received, qint64 total, QVariant userData);
    void whenAnchorClicked(const QUrl &link);
private:
    class chat_bg{
    public:
        QPixmap topLeft;
        QPixmap topCenter;
        QPixmap topRight;
        QPixmap rightCenter;
        QPixmap bottomRight;
        QPixmap bottomCenter;
        QPixmap bottomLeft;
        QPixmap leftCenter;
        QPixmap arrow;
        QColor center;
    };
    chat_bg chatto_bg;
    chat_bg chatfrom_bg;
    void addPixmapResource(const QUrl &resourceName, const QPixmap &pixmap);
    void addImageResource(const QUrl &resourceName, const QImage &image);
    QTextCursor appendTable(const QPixmap &icon, const QString &name, qint64 timestamp, QTextCursor (ChatTextViewer::*insert)(QTextCursor &, const QPixmap &, const QString &));
    QTextCursor insertTo(QTextCursor &cursor, const QPixmap &icon, const QString &name);
    QTextCursor insertFrom(QTextCursor &cursor, const QPixmap &icon, const QString &name);
    QStringList waitingList;
    qint64 lastTimeStamp;
    QMap<QByteArray, int> midMap;
};

#endif // CHATTEXTVIEWER_H
